package jmine.tec.aom.services.include;

import jmine.tec.aom.domain.PropertyType;
import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

/**
 * Includes a new {@link PropertyType}.
 * 
 * @author jrenaut
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class PropertyTypeService implements ServiceFiller<PropertyType> {

    public static final String MNEMONIC = "Mnemonic";

    public static final String DATABASE_ID = "Database ID";

    public static final String PROPERTY_VALUE_CLASS = "Property Value Class";

    private PackEntityPersistenceHelper entityHelper;

    /**
     * Finds or creates the given {@link PropertyType}
     * 
     * @param id the database id for this {@link PropertyType}
     * @param mnemonic the mnemonic for this {@link PropertyType}
     * @param propertyValueClass the property value class for this {@link PropertyType}
     * @throws PackException if an existent entry for this id is found and does not match the {@link PropertyType} type
     */
    @Execution
    public void execute(@Input(fieldName = DATABASE_ID) Long id, @Input(fieldName = MNEMONIC) String mnemonic,
            @Input(fieldName = PROPERTY_VALUE_CLASS) String propertyValueClass) throws PackException {
        PropertyType propertyType = this.entityHelper.findOrCreate(PropertyType.class, id, mnemonic);
        propertyType.setMnemonic(mnemonic);
        propertyType.setPropertyValueClass(propertyValueClass);
        this.entityHelper.save(propertyType, id);
    }

    /**
     * @param entityHelper the entityHelper to set
     */
    @Injected
    public void setEntityHelper(PackEntityPersistenceHelper entityHelper) {
        this.entityHelper = entityHelper;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, PropertyType entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.put(bean, MNEMONIC, entity.getMnemonic());
        ExportUtils.put(bean, PROPERTY_VALUE_CLASS, entity.getPropertyValueClass());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
    }

}
